<?php
/**
 * Class file for class SwiftBase
 *
 * @package    SwiftORM
 * @author     Stephen Gray
 * @copyright  Stephen Gray
 * @license    http://www.gnu.org/licenses/gpl.txt
 * @link       http://code.google.com/p/swiftorm
 */

/**
 * SwiftBase
 *
 * acts as a base database object
 *
 * @package    SwiftORM
 * @author     Stephen Gray
 * @copyright  Stephen Gray
 * @license    http://www.gnu.org/licenses/gpl.txt
 * @link       http://code.google.com/p/swiftorm
 */
class SwiftBase
{
    /**
     * $_config
     *
     * array of database/site config
     *
     * @var    array
     * @access protected
     */
    protected $_config = array();
    
    /**
     * $_conn
     *
     * resource for current connection
     *
     * @var    resource
     * @access protected
     */
    protected $_conn;

    /**
     * $_lastQuery
     *
     * string of last query run
     *
     * @var    string
     * @access protected
     */
    public $lastQuery;
    
    /**
     * void __construct ( array $config )
     *
     * constructor, saves config locally
     * and creates connection resource
     *
     * @param  array $config
     * @return void
     * @access public
     */
    public function __construct(array $config)
    {
        /**
         * store the config
         */
        $this->_config = $config;

        /**
         * connect
         */
        $this->_conn = mysql_connect($config['database']['host'], $config['database']['user'], $config['database']['pass']);
        mysql_select_db($config['database']['name'], $this->_conn);
    }

    /**
     * mixed _query ( string $query )
     *
     * runs query, saves it, return false if
     * bad query otherwise associative array
     * of results
     *
     * @param  steing $query
     * @return mixed
     * @access public
     */
    public function _query($query)
    {
        $this->lastQuery = $query;
        $results = mysql_query($query, $this->_conn);
        if ($results)
        {
            if (!is_resource($results))
            {
                return true;
            }
            
            $resultsArray = array();
            while ($row = mysql_fetch_assoc($results))
            {
                $resultsArray[] = $row;
            }
            return $resultsArray;
        }
        else
        {
            return false;
        }
    }

    /**
     * resource getConnection ()
     *
     * return current connection resource
     *
     * @return resource
     * @access public
     */
    public function getConnection()
    {
        return $this->_conn;
    }
}
